<template>
  <el-form label-width="110px" ref="formRef" :model="form" :rules="formRules">
    <el-row>
      <el-col :span="12">
        <el-form-item label="岗位标识" prop="code">
          <el-input :disabled="form.id && (form.code === 'TEN_SUPER_POST' || form.code === 'SYS_SUPER_POST')" clearable placeholder="请输入岗位标识"
                    v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="岗位名称" prop="name">
          <el-input clearable placeholder="请输入岗位名称" v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="数据权限范围" prop="scope">
          <el-select :disabled="form.id && (form.code === 'TEN_SUPER_POST' || form.code === 'SYS_SUPER_POST')" v-model="form.scope"
                     placeholder="请选择数据权限范围">
            <el-option key="ALL" label="全部数据权限" value="ALL"></el-option>
            <el-option key="DEPT" label="本部门数据权限" value="DEPT"></el-option>
            <el-option key="CHILD" label="本部门及以下数据权限" value="CHILD"></el-option>
            <el-option key="CUSTOM" label="自定义数据权限" value="CUSTOM"></el-option>
            <el-option key="SELF" label="仅本人数据权限" value="SELF"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" v-if="form.code !== 'TEN_SUPER_POST' && form.scope === 'CUSTOM'">
        <el-form-item label="数据权限列表" prop="deptIdList">
          <el-tree-select ref="dataTreeSelect" size="large" clearable multiple show-checkbox check-strictly
                          highlight-current collapse-tags collapse-tags-tooltip check-on-click-node node-key="id"
                          style="width: 100%" :render-after-expand="false" :data="deptList" :props="categoryProps"
                          v-model="form.deptIdList"
          ></el-tree-select>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="是否启用" prop="enabled">
          <el-radio-group v-model="form.enabled" :disabled="form.id && (form.code === 'TEN_SUPER_POST' || form.code === 'SYS_SUPER_POST')">
            <el-radio-button :value="true" border>启用</el-radio-button>
            <el-radio-button :value="false" border>停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" controls-position="right" size="large"
                           placeholder="请输入排序"></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="js">
import {onMounted, reactive, ref} from "vue";
import tree from "@/utils/tree";
import deptApi from "@/api/system/dept";

const props = defineProps({
  form: {
    type: Object,
    default: {}
  }
})
const formRef = ref();
const categoryProps = {
  label: (data) => data.name + "-" + data.code,
  children: 'children',
}
const deptList = ref([]);
const formRules = reactive({
  code: [{required: true, message: "岗位标识为必填项", trigger: "blur"}],
  name: [{required: true, message: "岗位名称为必填项", trigger: "blur"}],
  scope: [{required: true, message: "数据权限范围为必填项", trigger: "blur"}],
  deptIdList: [{required: true, message: "数据权限列表为必填项", trigger: "blur"}],
  enabled: [{required: true, message: "是否启用为必填项", trigger: "blur"}],
  sort: [{required: true, message: "排序为必填项", trigger: "blur"}],
});
const getFormRef = () => {
  return formRef.value;
}

onMounted(() => {
  deptApi.lists.get({enabled: true}).then(response => {
    deptList.value = tree.buildTree(response.data.list);
  })
});

defineExpose({getFormRef});
</script>
